import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { APP_FILTER, APP_GUARD } from '@nestjs/core';
import { ConfigModule } from '@nestjs/config';
import { LoggerDemoMiddleWare } from './common/middleware/logger.demo.middleware';
import { StoreModule } from './modules/store/store.module';
import { AllExceptionsFilter } from './common/filters/allExceptionFilter';
import { TestAuthGuard } from './common/guards/test';
import databaseConfig from './config/database.config';
import appConfig from './config/app.config';
// import { DatabaseModule } from './libs/database.module';
import { UserModule } from './modules/user/user.module';
import { DrizzleModule } from './libs/drizzle.module';
// import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    // 架构
    // todo: 迁移的lib目录下
    ConfigModule.forRoot({
      isGlobal: true,
      load: [appConfig, databaseConfig],
      envFilePath: `.env.${process.env.NODE_ENV}`,
    }),
    DrizzleModule,
    // DatabaseModule,
    // 业务
    StoreModule,
    UserModule,
  ],
  providers: [
    {
      provide: APP_FILTER,
      useClass: AllExceptionsFilter,
    },
    {
      provide: APP_GUARD,
      useClass: TestAuthGuard,
    },
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerDemoMiddleWare).forRoutes('*');
  }
}
